package educrypt.gui.components;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSeparator;

import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.CryptanalysisHillView;
import educrypt.gui.view.CryptanalysisOptionsView;
import educrypt.gui.view.CryptanalysisPlayfairView;
import educrypt.i18n.Messages;
import java.awt.event.ActionEvent;

class CryptanalysisOptionsPanel extends EducryptPanel implements
		CryptanalysisOptionsView {

	private static final long serialVersionUID = 6743705558673154220L;

	private JComboBox cbxCipherType, cbxAlphabet;
	private JButton btnBreak;

	private CryptpanalysisHillDialog cryptanalysisHillDialog;

	private CryptpanalysisPlayfairDialog cryptanalysisPlayfairDialog;

	public CryptanalysisOptionsPanel() {
		this.setSize(740, 80);
		this.setPreferredSize(this.getSize());
		setLayout(null);

		JLabel lblAlphabet = new JLabel(Messages.getString("guiAlphabet"));
		lblAlphabet.setBounds(355, 42, 67, 14);
		add(lblAlphabet);

		JLabel lblCipherType = new JLabel(Messages.getString("guiCipherType"));
		lblCipherType.setBounds(31, 42, 126, 14);
		add(lblCipherType);

		cbxCipherType = new JComboBox();
		cbxCipherType.setBounds(149, 39, 170, 20);
		add(cbxCipherType);

		JLabel lblCryptanalysisOptions = new JLabel(
				Messages.getString("guiCryptanalysisOptions"));
		lblCryptanalysisOptions.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblCryptanalysisOptions.setBounds(31, 11, 220, 19);
		add(lblCryptanalysisOptions);

		cbxAlphabet = new JComboBox();
		cbxAlphabet.setBounds(420, 39, 170, 20);
		add(cbxAlphabet);

		btnBreak = new JButton(Messages.getString("guiBreak"));
		btnBreak.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnBreak.setBounds(610, 34, 100, 30);
		add(btnBreak);

		JSeparator separator = new JSeparator();
		separator.setBounds(190, 79, 359, 1);
		add(separator);

		setPanelEnabled(false);

		cryptanalysisHillDialog = new CryptpanalysisHillDialog(
				GuiUtil.findParentFrame(this));

		cryptanalysisPlayfairDialog = new CryptpanalysisPlayfairDialog(
				GuiUtil.findParentFrame(this));
	}

	@Override
	public void addListener(ActionListener ctr) {
		cbxCipherType.setActionCommand(CIPHER_SELECTED);
		cbxCipherType.addActionListener(ctr);

		cbxAlphabet.setActionCommand(ALPHABET_SELECTED);
		cbxAlphabet.addActionListener(ctr);

		btnBreak.setActionCommand(BREAK);
		btnBreak.addActionListener(ctr);

		cryptanalysisHillDialog.addListener(ctr);
		cryptanalysisPlayfairDialog.addListener(ctr);
	}

	@Override
	public void setPanelEnabled(boolean b) {
		super.setPanelEnabled(b);
		if (b == true) {
			cbxAlphabet.setEnabled(false);
			btnBreak.setEnabled(false);
			cbxCipherType.setSelectedIndex(0);

		}
	}

	@Override
	public void enableBreak(boolean show) {
		btnBreak.setEnabled(show);

	}

	@Override
	public void showCiphers(List<String> ciphers) {
		GuiUtil.addItemsMaintainingSelected(cbxCipherType, ciphers);
	}

	@Override
	public int getSelectedCipherIndex() {
		return cbxCipherType.getSelectedIndex();
	}

	@Override
	public void showAlphabetsIfEnabled(List<String> alphabetNames) {
		if (cbxAlphabet.isEnabled()) {
			List<String> tmp = new ArrayList<String>(alphabetNames);
			tmp.add(0, Messages.getString("guiSelectAlphabet"));
			GuiUtil.addItemsMaintainingSelected(cbxAlphabet, tmp);
		}
	}

	@Override
	public void showAlphabets(List<String> alphabetNames) {
		cbxAlphabet.setEnabled(true);
		List<String> tmp = new ArrayList<String>(alphabetNames);
		tmp.add(0, Messages.getString("guiSelectAlphabet"));
		GuiUtil.addItemsMaintainingSelected(cbxAlphabet, tmp);
	}

	@Override
	public void hideAlphabets() {
		if (cbxAlphabet.isEnabled()) {
			assert cbxAlphabet.getModel().getSize() > 0;
			cbxAlphabet.setSelectedIndex(0);
			cbxAlphabet.setEnabled(false);
		}
	}

	@Override
	public int getSelectedAlphabetIndex() {
		return cbxAlphabet.getSelectedIndex();
	}

	@Override
	public String getSelectedAlphabetName() {
		return cbxAlphabet.getSelectedItem().toString();
	}

	@Override
	public CryptanalysisHillView getCryptanalysisHillDialog() {
		return this.cryptanalysisHillDialog;
	}

	@Override
	public CryptanalysisPlayfairView getCryptanalysisPlayfairDialog() {
		return this.cryptanalysisPlayfairDialog;
	}
}
